import 'package:flutter_admin/api/odoo/product_productionorderdetail_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input_num.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:quiver/strings.dart';

class BatchReturnProducts extends StatefulWidget {

  final List dataList;
  BatchReturnProducts({Key? key,required this.dataList}) : super(key: key);
  @override
  _BatchReturnProductsState createState() =>
      _BatchReturnProductsState();
}

class _BatchReturnProductsState extends State<BatchReturnProducts> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  String? note = "";

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var table = DataTable(
      columns: [
        DataColumn(label: Text('仓库位置')),
        DataColumn(label: Text('物料编号')),
        DataColumn(label: Text("物料名称")),
        DataColumn(label: Text("规格型号")),
        DataColumn(label: Text("数量")),
        DataColumn(label: Text("计量单位")),
      ],
      rows: widget.dataList.map((map) {
        return DataRow(
            cells: [
              DataCell(Text(map["location"]["name"]!=false?map["location"]["name"].toString():"")),
              DataCell(Text(map["product"]["default_code"]!=false?map["product"]["default_code"].toString():"")),
              DataCell(Text(map["product"]["name"]!=false?map["product"]["name"].toString():"")),
              DataCell(Text(map["product"]["description"]!=false?map["product"]["description"].toString():"")),
              DataCell(CryInputNum(
                width: 200,
                padding: 0,
                contentPadding: 0,
                value: map["quantity"],
                onChange: (v){
                  map["quantity"] = v;
                },
                validator: (v) {
                  return !isBlank(v) ? double.parse(v!) == 0? "不能为0": null : null;
                },
              )),
              DataCell(Text(map["uom"]['name']!=false?map["uom"]['name'].toString():"")),
            ]
        );
      }).toList(),
    );
    var form = Form(
      key: formKey,
      child: Column(
        children: <Widget>[
          CryInput(
            label: '退货原因',
            value: note,
            onChange: (v) {
              note = v;
            },
          ),
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child:table,
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            var product = widget.dataList.map((e) => {"id":e["product"]["id"],"num":num.parse(e["quantity"])}).toList();
            var domain = [
              ['location', '=', widget.dataList[0]['location']['id']],
              ['note', '=', note],
              ['pro', '=' , product]
            ];
            print(domain);
            ProductProductionorderdetailOdooApi.batchReturn(
                domain)
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //package:flutter_admin/pages/common/cry(re s.message!);
                return;
              }
              if(res.data["code"]!=200){
                CryUtils.message(res.data["message"]);
                return ;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("退货确认"),
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 10),
            form,
          ],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
  }

}
